import roleStore from '@/store/role-play.js'
import store from '@/store/index.js'
// 白名单 不需要token
const whiteList = ['/pages/login/login', '/pages/login/forget', '/pages/login/register', '/pages/login/wxphone', '/pages/pages_network/login/login', '/pages/pages_network/login/forget', '/pages/pages_network/login/register', '/pages/pages_network/login/wxphone']
const whiteList_net = ['/pages/pages_network/login/login', '/pages/pages_network/login/forget', '/pages/pages_network/login/register', '/pages/pages_network/login/wxphone']

const list = ["navigateTo", "redirectTo", "reLaunch", "switchTab"]
const dataValue = uni.getStorageSync('dd-access-user')
const token = dataValue.token//获取token值
function hasPermission(url) {
	// 在白名单中或有token，直接跳转
	if (whiteList.indexOf(url) !== -1 || uni.getStorageSync('dd-access-user').token) {
		return true;
	}
	return false;
}
function hasPermission_net(url) {
	// 在白名单中或有token，直接跳转
	if (whiteList.indexOf(url) !== -1 || uni.getStorageSync('dd-access-network').token) {
		return true;
	}
	return false;
}
const routeInterceptor = async () => {

	list.forEach((item) => {
		uni.addInterceptor(item, {
			// 页面跳转前进行拦截, invoke根据返回值进行判断是否继续执行跳转
			invoke(e) {
				// 获取要跳转的页面路径（url去掉"?"和"?"后的参数）
				const url = e.url.split('?')[0]
				if (hasPermission_net(url)) return true
				if (roleStore.state.rolePlay == 'u-user') {
					const datetime = new Date().getTime() //当前时间戳
					const dataValue = uni.getStorageSync('dd-access-user')
					const token = dataValue.token //获取token值
					const logintime = dataValue.logintime //获取时登录时间戳
					const timeToken = dataValue.timeToken//过期时间(秒)
					if (!(store.state.userMobileHasLogin || token)) return true
					if (token) {
						if (((datetime - logintime) / 1000) > timeToken) {
							uni.showModal({
								title: '提示',
								content: '登录过期，请重新登录！',
								showCancel: false,
							})
								.then(res => {
									//确认
									if (res.confirm) {
										//关闭所有页面，打开到应用内的某个页面
										uni.reLaunch({
											url: "/pages/login/login?custCode=" + uni.getStorageSync('custCode')
										});
										roleStore.commit('set_rolePlay', 'u-user')
										//关闭所有页面，打开到应用内的某个页面
										uni.removeStorageSync('dd-access-user')
									}
								})
						}
					}
					// 将用户的目标路径保存下来
					// 这样可以实现 用户登录之后，直接跳转到目标页面
					// uni.setStorageSync("URL", url)
					if (!hasPermission(url)) {
						uni.reLaunch({
							url: "/pages/login/login?custCode=" + uni.getStorageSync('custCode')
						});
						return false;
					}
				} else if (roleStore.state.rolePlay == 'u-network') {
					const datetime = new Date().getTime() //当前时间戳
					const dataValue = uni.getStorageSync('dd-access-network')
					const token = dataValue.token //获取token值
					const logintime = dataValue.logintime //获取时登录时间戳
					const timeToken = dataValue.timeToken//过期时间(秒)
					if (token) {
						if (((datetime - logintime) / 1000) > timeToken) {
							uni.showModal({
								title: '提示',
								content: '登录过期，请重新登录！',
								showCancel: false,
							})
								.then(res => {
									//确认
									if (res.confirm) {
										//关闭所有页面，打开到应用内的某个页面
										uni.reLaunch({
											url: "/pages/pages_network/login/login"
										});
										roleStore.commit('set_rolePlay', 'u-network')
										//关闭所有页面，打开到应用内的某个页面
										uni.removeStorageSync('dd-access-network')
									}
								})
						}
					}
					// 将用户的目标路径保存下来
					// 这样可以实现 用户登录之后，直接跳转到目标页面
					// uni.setStorageSync("URL", url)
					if (!hasPermission_net(url)) {
						uni.reLaunch({
							url: "/pages/pages_network/login/login"
						});
						return false;
					}
				}
				return true;
			}
		});
	})
}
export default routeInterceptor